package com.zhilei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.zhilei.enums.Sex;
import com.zhilei.enums.ShowWhichName;
import com.zhilei.enums.UserRole;
import com.zhilei.feign.ResumeMicroServiceFeign;
import com.zhilei.mapper.UsersMapper;
import com.zhilei.mq.InitResumeMQConfig;
import com.zhilei.pojo.Users;
import com.zhilei.pojo.bo.RegistLoginBO;
import com.zhilei.service.UsersService;
import com.zhilei.untils.DesensitizationUtil;
import com.zhilei.untils.LocalDateUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 志磊
 * @since 2024-06-29
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Autowired
    private ResumeMicroServiceFeign resumeMicroServiceFeign;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private InitResumeMQConfig initResumeMQConfig;

    /**
     * {@inheritDoc} 根据手机号查询用户是否存在
     *
     * @param mobile
     * @return
     */
    @Override
    public Users getByMobile(String mobile) {
        return baseMapper.selectOne(new QueryWrapper<Users>().eq("mobile", mobile));
    }

    /**
     * {@inher} 用户注册
     *
     * @param registLoginBO
     * @return
     */
    @Transactional
    @Override
    public Users register(RegistLoginBO registLoginBO) {
        String mobile = registLoginBO.getMobile();

        Users user = new Users();
        // 注册设置手机号
        user.setMobile(mobile);
        // 对手机号进行脱敏
        String desMobile = DesensitizationUtil.commonDisplay(mobile);
        // 注册设置昵称，格式为：用户+手机号
        user.setNickname("用户" + desMobile);
        // 注册设置真实姓名，格式为：用户+手机号
        user.setRealName("用户" + desMobile);
        // 注册设置对外展示的姓名是昵称
        user.setShowWhichName(ShowWhichName.nickname.type);
        // 注册设置性别为保密
        user.setSex(Sex.secret.type);
        // 注册设置头像为空
        user.setFace("https://img1.pconline.com.cn/piclib/200804/27/batch/1/1729/1209310588678ilj9zu1h9x.jpg");
        user.setEmail("");
        // 设置生日，给一个默认的时间,默认的时间是1900-01-01
        LocalDate birthdayDate = LocalDateUtils.parseLocalDate("1900-01-01",
                LocalDateUtils.DATE_PATTERN);
        user.setBirthday(birthdayDate);

        user.setCountry("中国");
        user.setProvince("");
        user.setCity("");
        user.setDistrict("");

        user.setDescription("这家伙很懒什么都没有留下！");

        // 我参加工作的时间
        user.setStartWorkDate(LocalDate.now());
        user.setPosition("");
        user.setRole(UserRole.CANDIDATE.type);
        user.setHrInWhichCompanyId("");
        user.setHrSignature("这家伙很懒什么都没有留下！");
        user.setHrTags("这家伙很懒什么都没有留下！");
        user.setCreatedTime(LocalDateTime.now());
        user.setUpdatedTime(LocalDateTime.now());

        // 录入数据表
        baseMapper.insert(user);

        // 注册成功后，初始化简历
        resumeMicroServiceFeign.init(user.getId());

        // 注册成功返回该用户
        return user;

    }

    /**
     * 使用MQ异步解耦发送消息，生成简历
     *
     * @param registLoginBO
     * @return
     */
    @Transactional
    @Override
    public Users createUserAndInitResumeMQ(RegistLoginBO registLoginBO) {
        Users user = register(registLoginBO);
        String userId = user.getId();
        rabbitTemplate.convertAndSend(InitResumeMQConfig.INIT_RESUME_EXCHANGE,
                InitResumeMQConfig.ROUTING_KEY_INIT_RESUME,
                new Gson().toJson(userId));
        return user;
    }
}
